# -*- coding: utf-8 -*-
'''
Created on 2012-8-22

@author: null
'''
from flask import Blueprint, render_template, current_app, redirect, url_for, jsonify
from flask.globals import request
from werkzeug.utils import secure_filename
from flaskext.babel import gettext
import os

main = Blueprint("main", __name__); 

@main.route("/")
def home():
    return render_template("home.html", name="User");

def allowed_file(filename): 
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in current_app.config['ALLOWED_EXTENSIONS'];
           
@main.route("/upload", methods=["POST"])
def upload():
    filename = "";
    error = "";
    if request.method == "POST":
        success = False; 
        file_stream = request.files['file'];

        if file_stream and allowed_file(file_stream.filename):
            filename = secure_filename(file_stream.filename);
            current_app.logger.debug(filename);
            file_stream.save(os.path.join(current_app.config['UPLOAD_FOLDER'], filename));
            success = True;
            return redirect(url_for('store.index', success=success, filename=filename));
        else:
            error = gettext(u"File format is not allowed.");
        
    resp = jsonify({"status": "OK", "success": success, "filename": filename, "error":error});
    return resp;
    